package cn.diancan.action.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.diancan.action.BaseAction;
import cn.diancan.dto.PurchaseSaleOrderByTypeDto;
import cn.diancan.dto.PurchaseSaleOrderProductDto;
import cn.diancan.dto.PurchaseSaleOrderStatisticsDto;
import cn.diancan.dto.query.PurchaseSaleOrderProductQuery;
import cn.diancan.manager.security.ManagerAuth;
import cn.diancan.manager.security.ManagerAuthUtil;
import cn.diancan.model.MealLocation;
import cn.diancan.model.PurchaseSaleOrder;
import cn.diancan.model.types.*;
import cn.diancan.util.DateUtil;
import cn.diancan.util.ObjectUtils;
import cn.diancan.vo.manager.PurchaseSaleManagerOrderActionVo;
import cn.diancan.vo.model.ProductVo;
import cn.diancan.vo.model.PurchaseSaleOrderProductVo;
import cn.diancan.vo.other.Pager;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;

public class PurchaseSaleOrderAction extends BaseAction implements ModelDriven<PurchaseSaleManagerOrderActionVo> {

    private PurchaseSaleManagerOrderActionVo requestVo = new PurchaseSaleManagerOrderActionVo();
    private Pager pager = new Pager();


    //1.输入条码（选择货号和颜色）--获取信息
    //2.输入商品件数
    //3.购物车， 增加数量 增加商品  删除商品，清空 
    @ManagerAuth
    public String createPurchaseSaleOrderPage() {
        try {
            List<ProductVo> list = purchaseSaleOrderBuyCarService.findProductVoBySysManagerId(ManagerAuthUtil.getValue());
            this.put("productList", list);

            return freemarkView("/views/ftl/manager/purchasesaleorder/create_purchase_sale_order.ftl");
        } catch (Exception e) {
            return managerError(e.getMessage());
        }

    }


    @ManagerAuth
    public String productInBuyCar() {
        try {
            List<ProductVo> list = purchaseSaleOrderBuyCarService.findProductVoBySysManagerId(ManagerAuthUtil.getValue());
            return jsonFormat(list);
        } catch (Exception e) {
            return jsonError(e);
        }
    }

    /**
     * 添加商品
     *
     * @return
     */
    @ManagerAuth
    public String addProduct() {
        try {
            boolean result = purchaseSaleOrderBuyCarService.saveAddProduct(ManagerAuthUtil.getValue(), requestVo.getProductId(), requestVo.getQuantity());
            return jsonFormat(result);
        } catch (Exception e) {
            return jsonError(e);
        }
    }

    /**
     * 修改商品数量
     *
     * @return
     */
    @ManagerAuth
    public String modifyProduct() {
        try {
            boolean result = purchaseSaleOrderBuyCarService.updateModifyProduct(ManagerAuthUtil.getValue(), requestVo.getProductId(), requestVo.getQuantity());
            return jsonFormat(result);
        } catch (Exception e) {
            return jsonError(e);
        }
    }

    //3.
    @ManagerAuth
    public String removeProduct() {
        try {
            boolean result = purchaseSaleOrderBuyCarService.updateModifyProduct(ManagerAuthUtil.getValue(), requestVo.getProductId(), 0);
            return jsonFormat(result);
        } catch (Exception e) {
            return jsonError(e);
        }
    }

    @ManagerAuth
    public String createPurchaseSaleOrder() {
        try {
            Long sysManagerId = ManagerAuthUtil.getValue();
            List<ProductVo> productVoList = purchaseSaleOrderBuyCarService.findProductVoBySysManagerId(sysManagerId);
            List<PurchaseSaleOrderProductVo> saleOrderProductVoList = new ArrayList<PurchaseSaleOrderProductVo>();
            if (null == productVoList || productVoList.size() <= 0) {
                return jsonError("请添加商品");
            }
            for (int i = 0; i < productVoList.size(); i++) {
                ProductVo productVo = productVoList.get(i);
                PurchaseSaleOrderProductVo vo = new PurchaseSaleOrderProductVo(productVo.getGoodsId(), productVo.getProductId(), productVo.getRetailPrice(), productVo.getQuantity());
                saleOrderProductVoList.add(vo);
            }

            PurchaseSaleOrder purchaseSaleOrder = purchaseSaleOrderService.saveCreatePurchaseOrder(saleOrderProductVoList, "", "", getIpAddr());
            purchaseSaleOrderBuyCarService.deleteAllBySysManagerId(sysManagerId);

            return jsonFormat(purchaseSaleOrder);
        } catch (Exception e) {
            return jsonError(e);
        }
    }

    @ManagerAuth
    public String saleOrderListPage() {
        try {
            pager.setPageNumber(requestVo.getPageNumber());
            pager.setPageSize(10);
            requestVo.setIsStatistics(CommonType.Statistics.YES);

            requestVo.handleByTimeOrderType();

            List<MealLocation> mealLocationList = mealLocationService.findMealLocationList(1, 100);


            PurchaseSaleOrderProductQuery query = requestVo.getQuery();
            PurchaseSaleOrderStatisticsDto statisticsDto = new PurchaseSaleOrderStatisticsDto();
            pager = purchaseSaleOrderService.findPagerBySaleOrder(pager, query, statisticsDto);

            this.put("purchaseSaleOrderProductStatusTypeEnum", PurchaseSaleOrderProductStatusType.getSaleValues());

            this.put("pager", pager);
            this.put("purchaseSaleOrderStatistics", statisticsDto);
            this.put("requestVo", requestVo);

            this.put("mealLocationList",mealLocationList);

            this.put("purchaseSaleOrderCategoryTypeEnum", PurchaseSaleOrderCategoryType.values());
            this.put("purchaseSaleOrderStatusTypeEnum", PurchaseSaleOrderStatusType.values());
            this.put("payTypeEnum", PayType.values());


            return freemarkView("/views/ftl/manager/purchasesaleorder/sale_order_list.ftl");
        } catch (Exception e) {
            e.printStackTrace();
            return managerError(e.getMessage());
        }
    }


    @ManagerAuth
    public String saleOrderProductListPage() {
        try {
            pager.setPageNumber(requestVo.getPageNumber());
            pager.setPageSize(10);
            requestVo.setIsStatistics(CommonType.Statistics.YES);

            requestVo.handleByTimeOrderType();



            PurchaseSaleOrderProductQuery query = requestVo.getQuery();
            PurchaseSaleOrderStatisticsDto statisticsDto = new PurchaseSaleOrderStatisticsDto();
            pager = purchaseSaleOrderService.findPagerBySaleOrderProduct(pager, query, statisticsDto);

            this.put("purchaseSaleOrderProductStatusTypeEnum", PurchaseSaleOrderProductStatusType.getSaleValues());

            this.put("pager", pager);
            this.put("purchaseSaleOrderStatistics", statisticsDto);
            this.put("requestVo", requestVo);


            return freemarkView("/views/ftl/manager/purchasesaleorder/sale_order_product_list.ftl");
        } catch (Exception e) {
            e.printStackTrace();
            return managerError(e.getMessage());
        }
    }




    @ManagerAuth
    public String saleOrderStatisticsProductListPage() {
        try {
            pager.setPageNumber(requestVo.getPageNumber());
            pager.setPageSize(10);


            requestVo.handleByTimeOrderType();


            requestVo.setIsStatistics(CommonType.Statistics.YES);
            //requestVo.setByOrderType(2);

            PurchaseSaleOrderProductQuery query = requestVo.getQuery2();
            //总量统计
            PurchaseSaleOrderStatisticsDto statisticsDto = new PurchaseSaleOrderStatisticsDto();

            pager = purchaseSaleOrderService.findPagerBySaleOrderProduct(pager, query, statisticsDto);


            this.put("purchaseSaleOrderProductStatusTypeEnum", PurchaseSaleOrderProductStatusType.getSaleValues());


            this.put("pager", pager);
            this.put("purchaseSaleOrderStatistics", statisticsDto);
            this.put("requestVo", requestVo);


            return freemarkView("/views/ftl/manager/purchasesaleorder/sale_order_statistics_product_list.ftl");
        } catch (Exception e) {
            e.printStackTrace();
            return managerError(e.getMessage());
        }
    }

    @ManagerAuth
    public String saleOrderStatisticsProductListAjax() {
        try {
            pager.setPageNumber(requestVo.getPageNumber());
            pager.setPageSize(100);

            if (null == requestVo.getStartTime()) {
                requestVo.setStartTime(DateUtil.currentDate() + " 00:00:00");
            }
            if (null == requestVo.getEndTime()) {
                requestVo.setEndTime(DateUtil.currentDate() + " 23:59:59");
            }
            requestVo.setIsStatistics(CommonType.Statistics.YES);
            //requestVo.setByOrderType(2);

            PurchaseSaleOrderProductQuery query = requestVo.getQuery2();
            //总量统计
            PurchaseSaleOrderStatisticsDto statisticsDto = new PurchaseSaleOrderStatisticsDto();

            pager = purchaseSaleOrderService.findPagerBySaleOrderProduct(pager, query, statisticsDto);

            List<PurchaseSaleOrderProductDto> list = (List<PurchaseSaleOrderProductDto>) pager.getList();
            if (null != list && list.size() > 0) {
                List<String> sizeList = new ArrayList<String>();//x-横向
                List<String> colorList = new ArrayList<String>();//y-纵向


                String[][] array = new String[100][100];//初始化
                for (int i = 0; i < list.size(); i++) {
                    PurchaseSaleOrderProductDto dto = list.get(i);
                    if (!colorList.contains(dto.getColorSpecificationValue())) {
                        colorList.add(dto.getColorSpecificationValue());
                    }
                    if (!sizeList.contains(dto.getSizeSpecificationValue())) {
                        sizeList.add(dto.getSizeSpecificationValue());
                    }
                    int x = colorList.indexOf(dto.getColorSpecificationValue());
                    int y = sizeList.indexOf(dto.getSizeSpecificationValue());
                    array[x][y] = "" + dto.getTotalQuantityCount();
                }
                this.put("sizeList", sizeList);
                this.put("colorList", colorList);
                this.put("pagerTable", array);
            }


            this.put("purchaseSaleOrderProductStatusTypeEnum", PurchaseSaleOrderProductStatusType.getSaleValues());


            this.put("pager", pager);
            this.put("purchaseSaleOrderStatistics", statisticsDto);
            this.put("requestVo", requestVo);


            return freemarkView("/views/ftl/manager/purchasesaleorder/sale_order_statistics_product_list2.ftl");
        } catch (Exception e) {
            e.printStackTrace();
            return managerError(e.getMessage());
        }
    }


    @ManagerAuth
    public String saleOrderStatisticsPage() {
        try {
            pager.setPageNumber(requestVo.getPageNumber());
            pager.setPageSize(10);

            requestVo.handleByTimeOrderType();

            requestVo.setIsStatistics(CommonType.Statistics.YES);
            //requestVo.setByOrderType(2);

            PurchaseSaleOrderProductQuery query = requestVo.getQuery();
            PurchaseSaleOrderStatisticsDto statisticsDto = new PurchaseSaleOrderStatisticsDto();



            List<PurchaseSaleOrderByTypeDto> orderCategoryStatistics = purchaseSaleOrderService.findStByOrderCategory(query);
            List<PurchaseSaleOrderByTypeDto> payTypeStatistics = purchaseSaleOrderService.findStByPayType(query);
            statisticsDto= purchaseSaleOrderService.findStatistics(query,statisticsDto);



            this.put("purchaseSaleOrderProductStatusTypeEnum", PurchaseSaleOrderProductStatusType.getSaleValues());

            this.put("pager", pager);

            this.put("requestVo", requestVo);
            this.put("purchaseSaleOrderStatistics", statisticsDto);

            this.put("orderCategoryStatistics", orderCategoryStatistics);
            this.put("orderCategoryStatisticsJson", JSON.toJSONString(orderCategoryStatistics));
            this.put("payTypeStatistics", payTypeStatistics);
            this.put("payTypeStatisticsJson", JSON.toJSONString(payTypeStatistics));

            this.put("purchaseSaleOrderCategoryTypeEnum", PurchaseSaleOrderCategoryType.values());
            this.put("payTypeEnum", PayType.values());


            return freemarkView("/views/ftl/manager/purchasesaleorder/sale_order_statistics.ftl");
        } catch (Exception e) {
            e.printStackTrace();
            return managerError(e.getMessage());
        }
    }




    @Override
    public PurchaseSaleManagerOrderActionVo getModel() {
        return requestVo;
    }


}
